
package ro.qi.framework.sys.entities.components;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import ro.qi.framework.sys.debug.Logger;
import ro.qi.framework.sys.entities.exception.ComponentException;

public class CollectionComponent implements EntityComponent {

	private Vector values = new Vector();
	private Properties elements = new Properties();

	/**
	 * @see ro.qi.framework.entities.components.EntityComponent#verify(java.lang.Object)
	 */
	public void verify(Object _value) throws ComponentException {
		if( !(_value instanceof ResultSet) )
			throw new ComponentException(this.getClass().getName() + ": " + values + " is not a ResultSet (" + _value.getClass().getName() + ")");
	}

	/**
	 * @see ro.qi.framework.entities.components.EntityComponent#setValue(java.lang.Object)
	 */
	public void setValue(Object _value) throws ComponentException {
		values.clear();
		verify(_value);
		ResultSet res = (ResultSet)_value;
		try {
			while( res.next() ) {
				Hashtable tmpHash = new Hashtable();
				Enumeration enum1 = elements.propertyNames();
				while( enum1.hasMoreElements() ) {
					String name = (String)enum1.nextElement();
					String field = elements.getProperty(name);
					if( field != null ) {
						try {
							Object tmpObj = res.getObject(field);
							//Logger.info(this, "name: " + name + " field: " + field + " : " + tmpObj);
							if( name != null && tmpObj != null )
								tmpHash.put(name, tmpObj);
						} catch (SQLException e) {
							Logger.error(this, "Error reading element " + name + " [" + field + "]: " + e.getMessage());
						}
					}
				}
				values.add(tmpHash);
			}
			res.close();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				if( res != null )
					res.close();
			} catch (SQLException e1) {}
			Logger.error(this, "Error reading elements " + e.getMessage());
		}
		// Log.info(this, "value has " + values.size() + " elements");
	}

	/**
	 * @see ro.qi.framework.entities.components.EntityComponent#getValue()
	 */
	public Object getValue() {
		if( values == null )
			return new Vector();
		return values;
	}

	/**
	 * @see ro.qi.framework.entities.components.EntityComponent#setConfig(org.w3c.dom.NodeList)
	 */
	public void setConfig(NodeList config) {
		if( config != null ) {
			for( int idx = 0 ; idx < config.getLength() ; idx++ ) {
				Node configNode = config.item(idx);
				if( configNode != null && configNode.getNodeType() == Node.ELEMENT_NODE ) {
					if( configNode.getNodeName() != null && configNode.getNodeName().toLowerCase().equals("element") && configNode.getAttributes() != null ) {
						if( configNode.getAttributes().getNamedItem("name") != null && configNode.getAttributes().getNamedItem("field") != null ) {
							String name = configNode.getAttributes().getNamedItem("name").getNodeValue();
							String field = configNode.getAttributes().getNamedItem("field").getNodeValue();
							elements.setProperty(name, field);
						}
					}
				}
			}
		}
	}

}
